LOG_FILE="vllm_lorax_$(date +%Y%m%d_%H%M%S).log"
MODEL_NAME="Qwen/Qwen2.5-7B-Instruct"
SERVED_NAME="Qwen2.5-7B-Instruct"
HOST="0.0.0.0"
PORT=8089
export MKL_SERVICE_FORCE_INTEL=1
kill_related_processes(){
    local port=$1
    local main_pid
    local child_pids=()
    local mp_child_pids=()
    # 检查端口是否被占用
    main_pid=$(lsof -ti tcp:$port)
    echo "main_pid: $main_pid"
    if [ -n "$main_pid" ]; then
        echo "Port $port is in use by PID $main_pid. Attempting to kill it an its related processes."
        # 将主进程PID添加到要终止的PID列表
        child_pids+=($main_pid)
        mp_child_pids=($(pgrep -f "multiprocessing.spawn"))
        echo "mp_child_pids: $mp_child_pids"
        for mp_pid in "${mp_child_pids[@]}"; do
            for child_pid in "${child_pids[@]}"; do
                if pgrep -P $child_pid | grep -q $mp_pid; then
                    child_pids+=($mp_pid)
                    break
                fi
            done
        done
        for pid in "${child_pids[@]}"; do
            echo "Found related PID $pid. Attempting to kill it."
            kill -9 $pid
        done
        sleep 2
    fi
}

kill_related_processes $PORT

CUDA_VISIBLE_DEVICES=0 vllm serve ${MODEL_NAME} \
    --host $HOST \
    --port $PORT \
    --served-model-name ${SERVED_NAME} \
    --gpu-memory-utilization 0.8 \
    --trust-remote-code \
    --enforce-eager \
    --tensor-parallel-size 1 \
    --max-model-len 4096 \
    --dtype half \
    --enable-lora \
    --max-loras 3 \
    --lora-modules classify=llm_finetune/saves/classify/checkpoint-600 \
    keywords=llm_finetune/saves/keywords/checkpoint-3300 \
    nlp2sql=llm_finetune/saves/nlp2sql/checkpoint-2300 \
    > ${LOG_FILE} 2>&1 &
